﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
namespace HXDNB.DAL
{
    public enum LogType
    {
        系统日志 = 1,
        添加日志 = 2,
        更新日志 = 3,
        删除日志 = 4
    }

    public class LogBLL
    {
        Entities E = new Entities();
        /// <summary>
        /// 根据ID查询单个日志详细信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Logs Get(int ID)
        {
            Logs Info = (from i in E.Logs where i.Id == ID select i).SingleOrDefault();
            return Info;
        }
        /// <summary>
        /// 查询日志信息列表
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public IQueryable<Logs> GetList(string type)
        {
            var list = from i in E.Logs select i;
            if (string.IsNullOrWhiteSpace(type) == false)
            {
                list = from i in list where i.Type == type select i;
            }
            list = from i in list orderby i.DateTime descending select i;
            return list;
        }
        /// <summary>
        /// 添加日志
        /// </summary>        
        /// <returns></returns>
        public bool Add(LogType LogType, string Msg, bool result, string ErrMsg)
        {
            try
            {
                Logs Model = new Logs();
                Model.DateTime = DateTime.Now;
                Model.Ip = HttpContext.Current.Request.UserHostAddress;
                Model.Location = HttpContext.Current.Request.Url.ToString();
                Model.Result = result.ToString();
                Model.DoUser = HttpContext.Current.Session["UserName"] == null ? "" : HttpContext.Current.Session["UserName"].ToString();
                Model.Type = LogType.ToString();
                Model.Message = Msg;
                if (result == false)
                {
                    Model.Message += string.Format("[#{0}#]", ErrMsg);
                }
                E.Logs.AddObject(Model);
                return E.SaveChanges() > 0;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 根据ID删除日志
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool Delete(DateTime time)
        {
            string strSql = "delete logs where DateTime<={0}";
            string err = null;
            int result = 0;
            try
            {
                result = E.ExecuteStoreCommand(strSql, time);
            }
            catch (Exception e)
            {
                err = e.Message;
                result = 0;
            }
            finally
            {
                string msg = string.Format("删除了[{0}]之前共{1}条日志", time.ToString(), result);
                new LogBLL().Add(LogType.系统日志, msg, result > 0, err);
            }
            return result > 0;
        }
    }
}
